
<?php
/**
 * class模板
 *
 * PHP version 7.2
 *
 * @category  API
 * @package   YII
 * @author    saruri <saruri@163.com>
 * @copyright 2006-2019 saruri
 * @license   https://saruri.cn/licence.txt BSD Licence
 * @link      http://saruri.cn
 * @date      2021/03/11 14:51:59
 */
//namespace console\models;
//use Yii;
require_once('arr.php');
class Country
{
    private $_config = [];
    //初始化
    public function __construct()
    {
        //各种配置
        new Terms();
        $re =new Terms();
        $this->countrys=$re->getCountryNameArr()->getTaxonomyArr()->getResult()->run();
        $this->mysql=$re->mysqli;
        $this->getTaxonomyArr();//所有关系表
        //var_dump($this->taxonomyArr);
        //exit("测试");
    }
      
    /*
    *  @desc      获取新增的 post
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:58:49
    */
    public function getPost()
    {
      
   
        //do somehing
        $where = " WHERE ID > 1145 ";
        $where = " WHERE ID > 4861 ";
        $where = " WHERE ID > 7063 ";
        $this->sqlname='摩尔多瓦';
       //CLS $where = " WHERE post_excerpt='德国'";
       $this->sqlname='波黑联系';
        $where = " WHERE post_excerpt='$this->sqlname'";
        //$where = " WHERE  `post_excerpt` IN ('波黑',' 伊朗','东帝汶','柬埔寨','阿联酋')"; //> 7062";//欧盟
        $where = " WHERE  `post_excerpt` IN ('波黑')"; //> 7062";//欧盟
        $SQL_SELECT_CODE="SELECT * FROM  wp_posts".$where;
        $CODE=$this->mysql->query($SQL_SELECT_CODE);
        while ($arr1=$CODE->fetch_array()) {
            $this->postArr[$arr1['ID']]=$arr1;
        }
        return $this;
    }

    /*
    *  @desc      获取分类 关系数组 term_id=>taxonomy_id
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/13 08:28:58
    */
    public function getTaxonomyArr()
    {
        //do somehing
        $where='';
        //$where = " WHERE ID > 1145 ";
        //$where = " WHERE taxonomy='favorites'";
        $SQL_SELECT_CODE="SELECT * FROM  wp_term_taxonomy".$where;
        $CODE=$this->mysql->query($SQL_SELECT_CODE);
        while ($arr1=$CODE->fetch_array()) {
            $this->taxonomyArr[$arr1['term_id']]=$arr1['term_taxonomy_id'];
        }
        //return $this;
    }



    /*
    *  @desc      gouzao 联系
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 17:36:27
    */
    public function build()
    {
        //do somehing
        $this->errMsg='';
        foreach ($this->postArr as $key => $value) {
            # code...
            $arr=$this->countrys[$value['post_excerpt']];
            if (!isset($arr)) {
                $this->errMsg2.='|'.$value['post_excerpt']."\r\n";
                ;
            }
            $post_id=$value['ID'];
            $taxonomy_id=$this->getTaxonomyId($arr['term_id']);
            $parent=$arr['parent'];
            if (!$taxonomy_id) {
                $this->errMsg.='|'.$value['post_excerpt']."\r\n";
                ;
            }
            //echo "构造了一次sql"."\r\n";
            $this->sql.=$this->buildInserSql($post_id, $taxonomy_id, $parent);
        }
        return $this;
    }



    /*
    *  @desc      根据获取taxonomy_id
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/13 08:25:13
    */
    public function getTaxonomyId($term_id)
    {
        //do somehing
        $taxonomyId='';
        $taxonomyId=$this->taxonomyArr[$term_id];
        return $taxonomyId;
    }

    /*
    *  @desc      构造插入sql
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 17:26:36
    */
    public function buildInserSql($post_id, $term_id, $parent)
    {
        //do somehing
        $SQL_INSERT_CODE='';
        if ($term_id=='') {
            return '';
        }
        //INSERT INTO `wp_term_relationships` (object_id,term_taxonomy_id) VALUES(1128 ,18); 1127 -> 18 //亚洲的代码  1127 ->211 //不丹的国家代码
        //INSERT INTO `wp_term_relationships` (object_id,term_taxonomy_id) VALUES(1128 ,18);
        $SQL_INSERT_CODE.="INSERT INTO `wp_term_relationships` (object_id,term_taxonomy_id) VALUES($post_id,$term_id);"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_term_relationships` (object_id,term_taxonomy_id) VALUES($post_id,$parent);"."\r\n";
        return $SQL_INSERT_CODE;
    }
      
    public function run()
    {
        //主流程
        //var_dump($this->countrys);
        //var_dump($this->postArr);
        //exit($this->errMsg2);
        //exit($this->errMsg);
        $this->save("relation".$this->sqlname, $this->sql);
        exit($this->sql);
    }
    //保存sql文件
    public function save($fileName, $string)
    {
        $newfolder  = 'c:\excel2';
        $fileName=$fileName.'_'.time();
        $fw = fopen($newfolder.'\\'.$fileName."relation.sql", "wr");
        //var_dump($fw);
        fputs($fw, $string);
        fclose($fw);
        return $this;
    }
}

$country = new country();
$country->getPost()->build()->run();
